package com.welab.pandahelper.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.welab.pandahelper.config.MyConfig;
import com.welab.pandahelper.bean.MyResponse;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class loginUtils {

    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static OkHttpClient client = new OkHttpClient();
    private static Gson gson = new Gson();
    private static SharedPreferences mPrefs;

    public static boolean checkLogin(Context context){
        mPrefs = context.getSharedPreferences(MyConfig.APP_NAME, Context.MODE_PRIVATE);
        Long userId = mPrefs.getLong("userId",0);
        if(userId == 0 || userId == null){
            return false;
        }
        Date date = new Date();
        Long now_time = date.getTime();
        Long login_time = mPrefs.getLong("loginTime",0);
        if ((now_time - login_time) >= (MyConfig.LOGIN_TIME * 1000)){
            Toast.makeText(context,"登录失效,请重新登录！",Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    public static boolean login(Context context, String username, String password) throws IOException {
        mPrefs= context.getSharedPreferences(MyConfig.APP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mPrefs.edit();
        Map<String,String> mm = new HashMap<>();
        mm.put("username",username);
        mm.put("password",password);
        String url = MyConfig.SERVER_HOST.concat(MyConfig.PATH_AUTH);
        RequestBody body = RequestBody.create(JSON, gson.toJson(mm));
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type","application/json")
                .addHeader("Accept","application/json")
                .post(body)
                .build();

        Response response = client.newCall(request).execute();
        Log.e(MyConfig.APP_NAME,response.code()+"");
        if (response.code() == 200){
            MyResponse j = gson.fromJson(response.body().string(), MyResponse.class);
            if ("0".equals(j.getErrorcode())){
                Date date = new Date();
                Long user_id = (Long) new Double((double)((Map)j.getData()).get("user_id")).longValue();
                String token = ((Map)j.getData()).get("token").toString();
                editor.putLong("userId",user_id);
                editor.putString("token",token);
                editor.putLong("loginTime",date.getTime());
                editor.commit();
                Log.e(MyConfig.APP_NAME,"登录成功");
                return true;
            }else {
                Log.e(MyConfig.APP_NAME,"登录失败1");
            }
        }else {
            Log.e(MyConfig.APP_NAME,"登录失败2");
        }
        return false;
    }

}
